package com.ylkj.patient.utils;

import static com.examination.mlib.constants.ProjectConfigure.Open.isOpenGroup;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.amap.api.location.AMapLocation;
import com.examination.mlib.bean.DefaultEntity;
import com.examination.mlib.constants.ARouterConstants;
import com.examination.mlib.constants.KitArouterConstants;
import com.examination.mlib.constants.UserInfoConstants;
import com.examination.mlib.interfaceapi.HomeBackApi;
import com.examination.mlib.utils.LocationUtils;
import com.example.homemodule.utils.HomeUtils;
import com.example.messagemodule.utils.MessageUtils;
import com.example.minemodule.Utils.MineUtils;
import com.mall.MallUtils;
import com.users.rn.rncommon.util.ReactUtils;
import com.yilijk.base.utils.ALog;
import com.yilijk.base.utils.SharedPreferencesUtils;
import com.yljk.live.common.utils.MediaCountyManager;
import com.ylkj.patient.rnmodule.RNRouterModule;
import com.ylkj.patient.ui.activities.MainActivity;
import com.ylkj.patientdrug.utils.DrugListUtils;

import java.util.ArrayList;
import java.util.Arrays;


/**
 * 推送消息的工具类
 */
public class PushMessageUtil {
    private static PushMessageUtil util = null;
    public static boolean imType = true;
    public static boolean homeType = true;

    private PushMessageUtil() {
    }

    public static PushMessageUtil getInstance() {
        if (util == null) {
            synchronized (PushMessageUtil.class) {
                if (util == null) {
                    util = new PushMessageUtil();
                }
            }
        }
        return util;
    }

    /**
     * 推送消息处理
     *
     * @param context
     * @param msg
     * @return
     */
    public boolean dealMessage(Context context, String msg) {
//        Log.e("shouldOverr", "dealmessage: " + msg);

//        if (TextUtils.isEmpty(msg) || !msg.startsWith("com.audituser.scheme://")) {
//        if (TextUtils.isEmpty(msg) || !(msg.startsWith("com.audituser.scheme://") || msg.startsWith("com.auditdoctor.scheme://"))) {
        if (TextUtils.isEmpty(msg)) {
            return false;
        }
        String token = SharedPreferencesUtils.getInstance().getString(UserInfoConstants.token, null);
        String passportId = SharedPreferencesUtils.getInstance().getString(UserInfoConstants.account_passportId, "");
        String mobile = SharedPreferencesUtils.getInstance().getString(UserInfoConstants.account_mobile, null);
        if (TextUtils.isEmpty(token) || TextUtils.isEmpty(mobile)) {
            return false;
        }
        ALog.e("oakkk", "msg1: " + msg);
        if (msg.contains("com.doctor.scheme://") || msg.contains("com.patient.scheme://")) {
            msg = msg.replaceFirst("//", "");
        }
        msg = msg.replaceFirst(":", "://");
        ALog.e("oakkk", "msg: " + msg);
        try {
            msg = msg.replace("#", "%23");
            Uri uri = Uri.parse(msg);
            String path = uri.getLastPathSegment();
            String host = uri.getHost();
            String id = uri.getQueryParameter("id");
            String shareId = uri.getQueryParameter("shareId");
            String load = uri.getQueryParameter("load");
            if (!TextUtils.isEmpty(shareId)) {
                RNRouterModule.getTemporaryDataStorage().put("shareIdKey", "{\"shareId\":\"" + shareId + "\"}");
            }
            ALog.e("oakkk：", "uri: " + uri + " " + path + " " + id + " " + host);
            String switchStr = path;
            if (TextUtils.isEmpty(switchStr)) {
                switchStr = host;
            }
            switch (switchStr) {
                //首页
                case "jumpToHomePage": {
                    Intent intent = new Intent(context, MainActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("toJumpMain", 0);
                    context.startActivity(intent);
                    return true;
                }
                //CMS活动落地页
                case "jumpToSalesActivity": {

                    ALog.e("oakkkkk", "jumpToSalesActivity: ");
                    if (!TextUtils.isEmpty(load) && (load.startsWith("http") || load.startsWith("https"))) {
                        String url = load.replace("${passportId}", passportId + "");
                        url = url.replace("${token}", token + "");
                        ALog.e("oakkkkk", "url: " + url);
                        ARouter.getInstance().build(ARouterConstants.MineWebviewActivity)
//                                                            .withString("title", bannerBean.getTitle())
                                .withBoolean("ishtml", false)
                                .withString("url", url)
                                .withBoolean("isShowLoading", true).navigation();
                    }
                    return true;
                }
                //优惠券页
                case "jumpToCouponsPage": {
                    AppReactUtils.INSTANCE.push(context, KitArouterConstants.MY_BUNDLE, KitArouterConstants.Mine.MyCoupon);
                    return true;
                }
                //直播
                case "jumpToLivePage": {
                    MediaCountyManager.getInstance().pageClick(msg);
                    return true;
                }
                case "cms": {
                    if (!TextUtils.isEmpty(load)) {
                        Bundle bundle = new Bundle();
                        bundle.putString("promotionId", load);
                        HomeBackApi homeBackApi = (HomeBackApi) ARouter.getInstance().build(ARouterConstants.HomeModuleImpl).navigation();
                        homeBackApi.Android2RnPush(context, KitArouterConstants.CART_DOCTORBUNDLE, KitArouterConstants.Cart.Promotion, bundle);
                    }
                    return true;
                }
                case "url": {
                    if (!TextUtils.isEmpty(load) && (load.startsWith("http") || load.startsWith("https"))) {
                        String url = load.replace("${passportId}", passportId + "");
                        url = url.replace("${token}", token + "");
                        ARouter.getInstance().build(ARouterConstants.MineWebviewActivity)
//                                                            .withString("title", bannerBean.getTitle())
                                .withBoolean("ishtml", false)
                                .withString("url", url)
                                .withBoolean("isShowLoading", true).navigation();
                    }
                    return true;
                }
                case "jumpToDrug": {
                    //快速购药
                    String doctorId = uri.getQueryParameter("doctorId");
                    if (!TextUtils.isEmpty(doctorId)) {
                        Bundle bundle = new Bundle();
                        bundle.putString("doctorId", doctorId);
                        HomeUtils.push(context, KitArouterConstants.CART_DOCTORBUNDLE, KitArouterConstants.Cart.Drug, bundle);
                    }
                    return true;
                }
                case "jumpToInquiry": {
                    String confirmId = uri.getQueryParameter("confirmId");
                    String dispatch = uri.getQueryParameter("dispatch");

                    Bundle bundle = new Bundle();
                    if (isOpenGroup && "3".equals(dispatch)) {
                        bundle.putString("confirmId", confirmId);
                        HomeUtils.push(context, KitArouterConstants.MY_BUNDLE, KitArouterConstants.Mine.Inquiry1, bundle);
                    } else {
                        bundle.putString("id", confirmId);
                        bundle.putString("dispatch", dispatch);
                        HomeUtils.push(context, KitArouterConstants.MY_BUNDLE, KitArouterConstants.Mine.PayChart, bundle);
                    }
                    return true;
                }
                case "jumpToDoctorStudio": {
                    //图文问诊
                    String doctorId = uri.getQueryParameter("doctorId");
                    if (!TextUtils.isEmpty(doctorId)) {
                        Bundle bundle = new Bundle();
                        bundle.putString("doctorId", doctorId);
                        bundle.putInt("selectIndex", 1);
                        MessageUtils.push(context,
                                KitArouterConstants.IM_DOCTORBUNDLE,
                                KitArouterConstants.Message.DoctorStudio,
                                bundle);
                    }
                    return true;
                }
                case "jumpToMyConsultation": {
                    HomeUtils.push(context, KitArouterConstants.MY_BUNDLE, KitArouterConstants.Mine.MyConsultation);
                    return true;
                }
                /**
                 * com.patient.scheme:Xinxin
                 */
                case "Xinxin": {
                    HomeUtils.push(context, KitArouterConstants.HOME_DOCTORBUNDLE, KitArouterConstants.Mine.Xinxin);
                    return true;
                }
                case "jumpToMyOrder": {
                    Bundle bundle = new Bundle();
                    bundle.putString("flag", "unpaid");
                    MineUtils.push(context, KitArouterConstants.MY_BUNDLE, KitArouterConstants.Mine.MyOrder, bundle);
                    return true;
                }
                case "jumpToShoppingCart": {
                    Intent intent = new Intent(context, MainActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("toJumpMain", 3);
                    context.startActivity(intent);
                    return true;
                }
                case "jumpToDrugDetail": {
                    String commodityType = uri.getQueryParameter("commodityType");
                    if (!TextUtils.isEmpty(commodityType) && !TextUtils.isEmpty(id)) {
                        String doctorId = uri.getQueryParameter("doctorId");
                        Bundle bundle = new Bundle();
                        if ("drug".equals(commodityType)) {
                            bundle.putString("id", "" + id);
                        } else if ("PT".equals(commodityType)) {
                            bundle.putString("commodityId", "" + id);
                        } else {
                            bundle.putString("packetId", "" + id);
                        }
                        if (!TextUtils.isEmpty(doctorId)) {
                            bundle.putString("doctorId", doctorId + "");
                        }
                        bundle.putString("commodityType", commodityType + "");
                        DrugListUtils.push(context,
                                KitArouterConstants.CART_DOCTORBUNDLE,
                                KitArouterConstants.Cart.DrugDetail,
                                bundle);
                        return true;
                    }
                    break;
                }
                case "jumpToRetailers": {
                    Bundle bundle = new Bundle();
                    String frontCategoryIds = uri.getQueryParameter("frontCategoryIds");
                    if (!TextUtils.isEmpty(frontCategoryIds)) {
                        String[] ids = frontCategoryIds.split(",");
                        ArrayList<String> idArray = new ArrayList<>(Arrays.asList(ids));
                        bundle.putStringArrayList("frontCategoryIds", idArray);
                    }
                    String doctorId = uri.getQueryParameter("doctorId");
                    if (!TextUtils.isEmpty(doctorId)) {
                        bundle.putString("doctorId", doctorId);
                    }
                    String whCity = uri.getQueryParameter("whCity");
                    if (!TextUtils.isEmpty(whCity)) {
                        bundle.putString("whCity", whCity);
                    }
                    DrugListUtils.push(context, KitArouterConstants.CART_DOCTORBUNDLE, KitArouterConstants.Cart.Retailers, bundle);

                    break;
                }
                case "general": {
                    Bundle bundle = new Bundle();
                    String compontName = "";
                    String pluginId = "";
                    // scheme分离获取入参string
                    String paramsString = msg.replace("com.doctor.scheme://general?", "");
                    // 参数截取
                    // 通过参数连接符‘&’获取参数数组
                    String[] paramsArray = paramsString.split("&");
                    for (int i = 0; i < paramsArray.length; i++) {
                        //每个数组对象为 key=value
                        String paramsObject = paramsArray[i];
                        String paramsKey = paramsObject.substring(0, paramsObject.indexOf("="));
                        String paramsValue = paramsObject.substring(paramsObject.indexOf("=") + 1);
                        if (paramsKey.equals("module")) {
                            pluginId = paramsValue;
                        } else if (paramsKey.equals("page")) {
                            compontName = paramsValue;
                        } else {
                            bundle.putString(paramsKey, paramsValue);
                        }
                    }
                    switch (pluginId) {
                        case "home":
                            pluginId = KitArouterConstants.HOME_DOCTORBUNDLE;
                            break;
                        case "cart":
                            pluginId = KitArouterConstants.CART_DOCTORBUNDLE;
                            break;
                        case "im":
                            pluginId = KitArouterConstants.IM_DOCTORBUNDLE;
                            break;
                        case "login":
                            pluginId = KitArouterConstants.LOGIN_DOCTORBUNDLE;
                            break;
                        case "my":
                            pluginId = KitArouterConstants.MY_BUNDLE;
                            break;
                        case "native":

                            return true;
                    }
                    AppReactUtils.INSTANCE.push(context, pluginId, compontName, bundle);
                    return true;
                }
            }

            return false;
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return false;
    }
}
